package com.miyu.system.service.open.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONReader;
import com.miyu.system.dao.open.SysCityDao;
import com.miyu.system.service.open.SysOpenApiService;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


/**
 * 系统开放api服务
 * @author miyu
 */
@Service
public class SysOpenApiServiceImpl implements SysOpenApiService {

    /** 系统城市数据层 */
    @Resource
    private SysCityDao sysCityDao;

    /**
     * 获取系统城市
     * @return {@link JSONArray}
     */
    @Override
    @Cacheable(cacheNames="system:sysCity" ,key="'sysCity'")
    public JSONArray getSysCity() {
       return JSONArray.parseArray(JSONArray.toJSONString(sysCityDao.findAll()), JSONReader.Feature.IgnoreSetNullValue) ;
    }

    @Override
//    @Cacheable(value  = "system:weather#3600",key="#param.getString('vCity')")
    public JSONObject getWeather(JSONObject param) {
        String key = param.getString("vCity");
        String version ="";
        version=param.getString("version");
        String city = param.getString("city");
        JSONObject rs = new JSONObject();
        //数据进行缓存 校验数据是否有 如果有是否超过两小时 如果超过一个小时更新 否者直接取值返回不再更新时间
        StringBuffer url = new StringBuffer();
        url.append("https://spot.yiketianqi.com/?");
        url.append("unescape=1");
        url.append("&version=").append(version);
        url.append("&appid=16319171&appsecret=Ywlwceu5");
        url.append("&cityid=").append(city);
            // 返回结果字符串
        String result = null;
        String result2= HttpUtil.get(url.toString(), CharsetUtil.CHARSET_UTF_8);
        System.out.println(result2);
//        WebService pj = new WebService();
//        pj.setUrl(url.toString());
//        pj.setMethod("GET");
//        result = WebHttpService.getWebHttpServiceStr(pj);-
//        if(result.length()>0) {
//            rs = JSONObject.parseObject(result);
//
//        }
        return rs;
    }
}
